﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IFViewDetail.KuaiQi.Others;
using ZedGraph;
using IFServerDLL;
using HCY.CTP.DBShangZheng;
using IFClientDLL;
using IFServerDLL.DB;

namespace FrmTryIF.Ctl.Calc.CtlDataImage
{
    public partial class CtlTick : CtlZedGraphBase
    {
        public CtlTick()
        {
            InitializeComponent();
        }
        public override void InitTryIF(ITryIF tryIF)
        {
            base.InitTryIF(tryIF);
            this.Text = "闪电图";
            InitChartGrap(zedGraphControl1.GraphPane);
            _tryif.DataServer.OnReceiveIFData += new Action<MyData_IF>(DataServer_OnReceiveIFData);
            _tryif.DataServer.OnReceiveMarketData += new Action<MyData_DaPan>(DataServer_OnReceiveMarketData);
            _tryif.DataServer.OnReceiveRedGreenBarData += new Action<MyData_RedGreen>(DataServer_OnReceiveRedGreenBarData);
        }

        void DataServer_OnReceiveRedGreenBarData(MyData_RedGreen obj)
        {
            ResponeRedGreenBarData(obj);
        }

        void DataServer_OnReceiveMarketData(MyData_DaPan obj)
        {
            ResponeHuShen300StockData(obj);
        }

        void DataServer_OnReceiveIFData(MyData_IF obj)
        {
            ResponeMarketData(obj);
        }

       
        YAxis _yAxis大盘;

        private void InitChartGrap(GraphPane grapPane)
        {
            grapPane.Fill = new ZedGraph.Fill(Color.Black);
            grapPane.Chart.Fill = new ZedGraph.Fill(Color.Black);
            grapPane.Chart.Border.Color = Color.Red;
            grapPane.XAxis.Type = AxisType.Date;
            grapPane.XAxis.Scale.Format = "T";
            grapPane.YAxis.MajorGrid.IsVisible = true;
            grapPane.XAxis.MajorGrid.IsVisible = true;
            //grapPane.YAxis.MinorGrid.IsVisible = true;
            grapPane.XAxis.Scale.IsSkipLastLabel = false;
            grapPane.XAxis.Scale.IsSkipFirstLabel = false;
            grapPane.YAxis.Scale.IsSkipFirstLabel = false;
            grapPane.YAxis.Scale.IsSkipLastLabel = false;
            grapPane.YAxis.Scale.FontSpec.Size = 12;
            grapPane.XAxis.Scale.FontSpec.Size = 12;
            grapPane.Y2Axis.Scale.FontSpec.Size = 12;
            grapPane.YAxis.Scale.Format = "P2";
            grapPane.YAxis.Scale.MagAuto = false;
            grapPane.XAxis.Scale.MagAuto = false;
            grapPane.Y2Axis.Scale.MagAuto = false;
            grapPane.Legend.IsVisible = false;
            grapPane.XAxis.Title.IsVisible = false;
            grapPane.YAxis.Title.IsVisible = false;
            grapPane.XAxis.MajorGrid.Color = Color.Red;
            grapPane.XAxis.MajorTic.Color = Color.Red;
            grapPane.YAxis.MajorGrid.Color = Color.Red;
            grapPane.YAxis.MajorTic.Color = Color.Red;
            grapPane.XAxis.Scale.FontSpec.FontColor = Color.Red;
            grapPane.YAxis.Scale.FontSpec.FontColor = Color.Red;
            grapPane.YAxis.Scale.MaxAuto = false;
            grapPane.YAxis.Scale.MinAuto = false;
            //grapPane.XAxis.Scale.MaxAuto = false;
            //grapPane.XAxis.Scale.MinAuto = false;
            //grapPane.XAxis.Scale.Max = 400;
            //grapPane.YAxis.Scale.MaxAuto = false;
            _priceLine股指 = grapPane.AddCurve("price", new PointPairList(), Color.White);
            _priceLine股指.Symbol.IsVisible = false;
            _priceLine股指.IsY2Axis = false;
            _red红柱 = grapPane.AddBar("Distance", new PointPairList(), Color.Red);
            _red红柱.YAxisIndex = 1;
            _red红柱.IsY2Axis = true;


            grapPane.XAxis.MajorGrid.IsZeroLine = false;
            grapPane.YAxis.MajorGrid.IsZeroLine = false;
            grapPane.Y2Axis.MajorGrid.IsZeroLine = false;
            grapPane.Y2Axis.Scale.MinAuto = false;
            grapPane.Y2Axis.Scale.MaxAuto = false;
            grapPane.Y2Axis.Scale.Min = -300;
            grapPane.Y2Axis.Scale.Max = 300;
            grapPane.Y2Axis.IsVisible = true;
            grapPane.Y2Axis.MajorTic.IsOpposite = false;
            grapPane.Y2Axis.MinorTic.IsOpposite = false;
            grapPane.Y2Axis.Scale.FontSpec.FontColor = Color.Red;
            grapPane.Y2Axis.Title.FontSpec.FontColor = Color.Red;
            // Display the Y2 axis grid lines
            //grapPane.YAxis.MajorGrid.IsVisible = true;
            //grapPane.YAxis.MajorGrid.IsZeroLine = true;
            grapPane.Y2Axis.MajorGrid.Color = Color.Red;
            grapPane.YAxis.Scale.Align = AlignP.Inside;
            grapPane.Y2Axis.Scale.Align = AlignP.Inside;
            grapPane.Y2Axis.Scale.IsVisible = false;



            _yAxis大盘 = new YAxis("iff");
            _yAxis大盘.IsVisible = true;
            _yAxis大盘.Scale.IsVisible = false;
            zedGraphControl1.GraphPane.YAxisList.Add(_yAxis大盘);
            _yAxis大盘.Scale.FontSpec.FontColor = Color.White;
            _yAxis大盘.Title.FontSpec.FontColor = Color.White;
            _yAxis大盘.MinorGrid.IsVisible = false;
            _yAxis大盘.IsVisible = false;
            _yAxis大盘.Color = Color.White;
            // turn off the opposite tics so the Y2 tics don't show up on the Y axis
            _yAxis大盘.MajorTic.IsInside = false;
            _yAxis大盘.MinorTic.IsInside = false;
            _yAxis大盘.MajorTic.IsOpposite = false;
            _yAxis大盘.MinorTic.IsOpposite = false;
            _yAxis大盘.Title.IsVisible = false;
            //_yAxis股指.Scale.MaxAuto = false;
            //_yAxis股指.Scale.MinAuto = false;
            _priceLine大盘 = zedGraphControl1.GraphPane.AddCurve("iff", new PointPairList(), Color.Yellow);
            _priceLine大盘.Symbol.IsVisible = false;

            YAxis yAxis绿珠 = new YAxis("green");

            yAxis绿珠.IsVisible = false;
            zedGraphControl1.GraphPane.YAxisList.Add(yAxis绿珠);
            yAxis绿珠.Color = Color.Green;
            yAxis绿珠.Scale.Max = 200;
            yAxis绿珠.Scale.Min = -200;
            _green柱 = zedGraphControl1.GraphPane.AddBar("green", new PointPairList(), Color.Green);
            _green柱.YAxisIndex = 2;

            
        }
        LineItem _priceLine股指;
        LineItem _priceLine大盘;
        BarItem _red红柱;
        BarItem _green柱;

        private double _first股指指数 = 0;
        private double _First大盘指数 = 0;

        private void SetYScale(double dt)
        {

            double min = 10000;
            double max = -10000;
            if (dt > 400)
            {

                double mindt = this.CalcXDataByDateTime(XDate.XLDateToDateTime(dt).AddMinutes(-1));
                zedGraphControl1.GraphPane.XAxis.Scale.Max = dt;
                zedGraphControl1.GraphPane.XAxis.Scale.Min = mindt;

                for (int i = _priceLine股指.Points.Count - 1; i >= 0; i--)
                {
                    if (_priceLine股指.Points[i].X < mindt)
                    {
                        break;
                    }
                    if (_priceLine股指.Points[i].Y > max)
                    {
                        max = _priceLine股指.Points[i].Y;
                    }
                    if (_priceLine股指.Points[i].Y < min)
                    {
                        min = _priceLine股指.Points[i].Y;
                    }
                }
                for (int i = _priceLine大盘.Points.Count - 1; i >= 0; i--)
                {
                    if (_priceLine大盘.Points[i].X < mindt)
                    {
                        break;
                    }
                    if (_priceLine大盘.Points[i].Y > max)
                    {
                        max = _priceLine大盘.Points[i].Y;
                    }
                    if (_priceLine大盘.Points[i].Y < min)
                    {
                        min = _priceLine大盘.Points[i].Y;
                    }
                }
            }




            zedGraphControl1.GraphPane.YAxis.Scale.Max = max + 0.0002;
            zedGraphControl1.GraphPane.YAxis.Scale.Min = min - 0.0002;
            try
            {
                zedGraphControl1.AxisChange();
                zedGraphControl1.Invalidate();
            }
            catch
            {

            }




        }
        private double _last大盘指数 = 0;
        private void ResponeHuShen300StockData(MyData_DaPan data)
        {
            if (_First大盘指数 == 0)
            {

                _First大盘指数 = (double)data.NewPrice;

            }
            _last大盘指数 = (double)data.NewPrice;
            double yValue = ((double)data.NewPrice - _First大盘指数) / _First大盘指数;
            int dt = GlobalStatic.GetIntByTime(data.DT);
            double xdt = this.CalcXDataByDateTime(data.DT);
            
            PointPair price = new PointPair(xdt, yValue);

            _priceLine大盘.AddPoint(price);


            //PointPair redgreen = new PointPair(XDate.DateTimeToXLDate(data.时间), (double)data.红绿柱);



            //if (data.红绿柱 > 0)
            //{
            //    _red红柱.AddPoint(redgreen);
            //}
            //else
            //{
            //    _green柱.AddPoint(redgreen);
            //}
        }

        private void ResponeRedGreenBarData(MyData_RedGreen data)
        {
            double xdt = this.CalcXDataByDateTime(data.DT);
            
            PointPair redgreen = new PointPair(xdt, (double)data.RedGreen);


            if (data.RedGreen > 0)
            {
                _red红柱.AddPoint(redgreen);
            }
            else
            {
                _green柱.AddPoint(redgreen);
            }

        }



        private bool _IsFirstBase股指 = false;

        private void ResponeMarketData(MyData_IF data)
        {

            double dt = this.CalcXDataByDateTime(data.MarketTime);
            
            if (_first股指指数 == 0)
            {
                _first股指指数 = (double)data.NewPrice;

            }

            if (_IsFirstBase股指 == false)
            {


                if (GlobalStatic.GetIntByTime( data.MarketTime) > 3620)
                {
                    _IsFirstBase股指 = true;
                    _first股指指数 = (double)data.NewPrice;
                    _First大盘指数 = (double)_last大盘指数;
                }
            }

            double yValue = ((double)data.NewPrice - _first股指指数) / _first股指指数;
            PointPair price = new PointPair(dt, yValue);
            _priceLine股指.AddPoint(price);

            SetYScale(dt);
        }
    }
}
